package com.sky.service.impl;

import com.sky.constant.StatusConstant;
import com.sky.entity.Orders;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.UserMapper;
import com.sky.mapper.WorkspaceMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.HashMap;
import java.util.Map;

@Service
@Slf4j
public class WorkspaceServiceImpl implements WorkspaceService {

    @Autowired
    private WorkspaceMapper workspaceMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private OrderMapper orderMapper;

    /**
     * 查询今日数据
     *
     * @return 今日数据
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime beginTime, LocalDateTime endTime) {
        // 构建时间范围
        Map<String, Object> map = this.getTimeRangeMap(beginTime, endTime);
        map.put("status", Orders.COMPLETED); // 设置订单状态为已完成
        // 根据条件查询今日数据
        BusinessDataVO businessDataVO = workspaceMapper.selectBusinessDataByMap(map);
        // 营业额 SELECT SUM(amount) FROM orders WHERE order_time BETWEEN #{begin} AND #{end} AND status = 5
        Double turnover = businessDataVO.getTurnover() != null ? businessDataVO.getTurnover() : 0.0;
        // 有效订单数 SELECT COUNT(id) FROM orders WHERE order_time BETWEEN #{begin} AND #{end} AND status = 5
        Integer validOrderCount = businessDataVO.getValidOrderCount();
        // 平均客单价 SELECT AVG(amount) FROM orders WHERE order_time BETWEEN #{begin} AND #{end} AND status = 5
        Double unitPrice = businessDataVO.getUnitPrice() != null ? businessDataVO.getUnitPrice() : 0.0;

        map.put("status", null); // 设置订单状态为空
        // 订单完成率 validOrderCount / totalOrderCount = SELECT COUNT(id) FROM orders WHERE order_time BETWEEN #{begin} AND #{end}
        businessDataVO = workspaceMapper.selectBusinessDataByMap(map);
        Integer totalOrderCount = businessDataVO.getValidOrderCount();
        Double orderCompletionRate = totalOrderCount != 0 ? (double) validOrderCount / totalOrderCount : 0.0;
        // 新增用户数 SELECT COUNT(id) FROM user WHERE create_time BETWEEN #{begin} AND #{end}
        Integer newUsers = userMapper.countByMap(map);

        // 封装返回数据
        return BusinessDataVO.builder()
                .turnover(turnover)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .unitPrice(unitPrice)
                .newUsers(newUsers)
                .build();
    }

    /**
     * 查询订单概览数据
     *
     * @return 订单概览数据
     */
    @Override
    public OrderOverViewVO getOverviewOrders() {
        // 构建今日时间范围
        Map<String, Object> map = this.getTimeRangeMap(LocalDateTime.now().with(LocalTime.MIN), LocalDateTime.now().with(LocalTime.MAX));
        // 根据状态查询订单数量
        map.put("status", Orders.TO_BE_CONFIRMED);
        Integer waitingOrders = orderMapper.countByMap(map);
        map.put("status", Orders.CONFIRMED);
        Integer deliveredOrders = orderMapper.countByMap(map);
        map.put("status", Orders.COMPLETED);
        Integer completedOrders = orderMapper.countByMap(map);
        map.put("status", Orders.CANCELLED);
        Integer cancelledOrders = orderMapper.countByMap(map);
        map.put("status", null);
        Integer allOrders = orderMapper.countByMap(map);
        // 封装返回数据
        return OrderOverViewVO.builder()
                .waitingOrders(waitingOrders)
                .deliveredOrders(deliveredOrders)
                .completedOrders(completedOrders)
                .cancelledOrders(cancelledOrders)
                .allOrders(allOrders)
                .build();
    }

    /**
     * 查询菜品总览
     *
     * @return 菜品总览
     */
    @Override
    public DishOverViewVO getOverviewDishes() {
        // 构建今日时间范围
        Map<String, Object> map = this.getTimeRangeMap(LocalDateTime.now().with(LocalTime.MIN), LocalDateTime.now().with(LocalTime.MAX));
        // 根据条件查询菜品总览
        map.put("status", StatusConstant.ENABLE);
        Integer sold = workspaceMapper.selectOverviewDishesByMap(map);
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = workspaceMapper.selectOverviewDishesByMap(map);
        // 封装返回数据
        return DishOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 查询套餐总览
     *
     * @return 套餐总览
     */
    @Override
    public SetmealOverViewVO getOverviewSetmeals() {
        // 构建今日时间范围
        Map<String, Object> map = this.getTimeRangeMap(LocalDateTime.now().with(LocalTime.MIN), LocalDateTime.now().with(LocalTime.MAX));
        // 根据条件查询套餐总览
        map.put("status", StatusConstant.ENABLE);
        Integer sold = workspaceMapper.selectOverviewSetmealsByMap(map);
        map.put("status", StatusConstant.DISABLE);
        Integer discontinued = workspaceMapper.selectOverviewSetmealsByMap(map);
        // 封装返回数据
        return SetmealOverViewVO.builder()
                .sold(sold)
                .discontinued(discontinued)
                .build();
    }

    /**
     * 获取今日时间范围 map
     *
     * @param beginTime 开始时间
     * @param endTime   结束时间
     * @return map
     */
    private Map<String, Object> getTimeRangeMap(LocalDateTime beginTime, LocalDateTime endTime) {
        Map<String, Object> map = new HashMap<>();
        map.put("begin", beginTime);
        map.put("end", endTime);
        return map;
    }
}
